﻿/**
 * 搜索插件
 */
(function(E){

function openForm(url){
	var form = $('#jqe-form-openlink');
	if( form.size() ){
		form = form[0];
	}else{
		form = document.createElement('form');
		form.id = 'jqe-form-openlink';
		form.style.display = 'none';
		form.target = '_blank';
		form.method = 'GET';
		$(form).appendTo('body');
	}
	
	form.action = url;
	form.submit();
	form.reset();
}

function openLink(url){
	var a = $('#jqe-a-openlink');
	if( !a.size() ){
		$('body').append('<a id="jqe-a-openlink" style="display:;" target="_blank">openlink</a>');
		a = $('#jqe-a-openlink');
	}
	
	a.attr('href', url);
	a.click();
}

function open(url){
	openForm(url);
}

E.plugin("BaikeSearch", {
	click: function(Menu, self, target){
		if( Menu ){
			var text = $.trim(self.getSelectionText() ),
				url = 'http://www.baike.com/wiki/'+encodeURI(text);
			open(url);
			return true;
		}
	}
});

E.plugin("BaiduSearch", {
	click: function(Menu, self, target){
		if( Menu ){
			var text = $.trim(self.getSelectionText() ),
				url = 'http://www.baidu.com/s?wd='+encodeURI(text);
			open(url);
			return true;
		}
	}
});

E.plugin("GoogleSearch", {
	click: function(Menu, self, target){
		if( Menu ){
			var text = $.trim(self.getSelectionText() ),
				url = 'http://www.google.com.hk/search?sourceid=chrome&ie=UTF-8&q='+encodeURI(text);
			open(url);
			return true;
		}
	}
});

})(jQEditor);